﻿
//#define OS_ANDROID
#define OS_WIN


namespace n_OS
{
using System;

#if OS_WIN
	using System.IO;
#endif

#if OS_ANDROID
	using test20140822;
	using Android.Widget;
	using Android.App;
#endif

public static class OS
{
	//系统文件所在根目录的父目录
	public static string SystemPrePath;
	
	//系统文件所在根目录
	public static string SystemRoot;
	
	//组件库根目录
	public static string ModuleLibPath;
	
	//用户组件库根目录
	public static string UserLibName;
	
	//编译器系统文件所在根目录
	public static string CompileRoot;
	
	//文件路径的分隔符
	public static string PATH_S;
	
	//文件路径的分隔符
	public static char PATH_SC;
	
	//用户配置符号
	public static string USER;
	
	//目前的用户
	public const string USER_linkboy = "linkboy";
	
	//是否有系统预设的用户
	public static string DefaultUser = null;
	public static string DefaultUserFile = null;
	
	//扩展名
	public static string LinkboyFile;
	
	//初始化
	public static void Init()
	{
		//-------------------------------------
		#if OS_ANDROID
			PATH_S = @"/";
			PATH_SC = '/';
			SystemRoot = "";
		#elif OS_WIN
			PATH_S = @"\";
			PATH_SC = '\\';
			SystemRoot = AppDomain.CurrentDomain.BaseDirectory;
		#else
			#error "操作系统未定义"
		#endif
		
		int i = SystemRoot.Remove( SystemRoot.Length - 1 ).LastIndexOf( PATH_SC );
		SystemPrePath = SystemRoot.Remove( i + 1 );
		
		//-------------------------------------
		//输入输出初始化
		VIO.Init();
		
		//-------------------------------------
		//路径无关项
		ModuleLibPath = SystemRoot + "Lib" + OS.PATH_S;
		CompileRoot = SystemRoot;
		string[] temp = VIO.OpenTextFile( SystemRoot + "Resource" + PATH_S + "config.txt" ).Split( '\n' )[0].Split( ' ' );
		
		UserLibName = "第三方模块库";
		
		if( DefaultUser == null ) {
			USER = temp[0];
			LinkboyFile = temp[1];
		}
		else {
			USER = DefaultUser;
			LinkboyFile = DefaultUserFile;
		}
	}
}
public static class VIO
{
	#if OS_WIN
		static System.Windows.Forms.RichTextBox r;
	#elif OS_ANDROID
		public static Activity ac;
	#endif
	
	//初始化
	public static void Init()
	{
		#if OS_WIN
			r = new System.Windows.Forms.RichTextBox();
		#endif
	}
	
	//打开RTF文件并提取文本
	public static string OpenRtfFile( string FileName )
	{
		r.LoadFile( FileName );
		return r.Text;
	}
	
	//打开文本文件
	public static string OpenTextFile( string FileName )
	{
		#if OS_WIN
			
			string s = File.ReadAllText( FileName, System.Text.Encoding.GetEncoding("gb2312") );
			return s.Replace( "\r\n", "\n" );
			
		#elif OS_ANDROID
			return A_IO.Open( FileName );
		#endif
	}
	
	//保存文本文件
	public static void SaveTextFile( string FileName, string Text )
	{
		Text = Text.Replace( "\r\n", "\n" );
		Text = Text.Replace( "\n", "\r\n" );
		System.IO.File.WriteAllText( FileName, Text, System.Text.Encoding.GetEncoding( "gb2312" ) );
	}
	
	//判断文件是否存在
	public static bool FileExists( string Path )
	{
		#if OS_WIN
			return File.Exists( Path );
		#elif OS_ANDROID
			return true;
		#else
			#error "操作系统未定义"
		#endif
	}
	
	//输出一个信息
	public static void Show( string m )
	{
		#if OS_WIN
			System.Windows.Forms.MessageBox.Show( "<" + m + ">" );
		#elif OS_ANDROID
			Toast.MakeText( ac, m, Android.Widget.ToastLength.Long ).Show();
		#endif
		
	}
}
}





